﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.RoleModules
{
    public class RoleUpdaterRegister
    {
        int rows;
        public SystemDataUpdater<RoleModel> GetRoleUpdater()
        {
            SystemDataUpdater<RoleModel> systemDataUpdater = new SystemDataUpdater<RoleModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        public SystemDataUpdater<RoleModuleModel> GetModuleRoleUpdater()
        {
            SystemDataUpdater<RoleModuleModel> systemDataUpdater = new SystemDataUpdater<RoleModuleModel>();
            systemDataUpdater.DataUpdating += SingletonValid;
            //systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        public SystemDataUpdater<RoleSiteModel> GetSiteRoleUpdater()
        {
            SystemDataUpdater<RoleSiteModel> systemDataUpdater = new SystemDataUpdater<RoleSiteModel>();
            systemDataUpdater.DataUpdating += SingletonValid;
            //systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        public SystemDataUpdater<RoleUserModel> GetUserRoleUpdater()
        {
            SystemDataUpdater<RoleUserModel> systemDataUpdater = new SystemDataUpdater<RoleUserModel>();
            systemDataUpdater.DataUpdating += SingletonValid;
            //systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<RoleModel> e)
        {
            if (e.Delete.Avaliable)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.Name);
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<RoleModel> e)
        {
            RoleFetcher roleFetcher = new RoleFetcher();
            string message = "";
            RoleModel role = roleFetcher.GetByCode(e.Insert.Code, out message);
            if (role != null && role.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被[{1}]占用", role.Code, role.Name);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<RoleModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "名称不能为空";
                e.Cancle = true;
                return;
            }
        }

        private void CheckAvaliable(object sender, DataValidationEventArgs<RoleModuleModel> e)
        {
            if (e.Delete.Avaliable)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<RoleModuleModel> e)
        {
            RoleModuleFetcher fetcher = new RoleModuleFetcher();
            string message = "";
            RoleModuleModel roleModule = fetcher.GetByRoleIdAndModuleId(e.Insert.RoleId, e.Insert.ModuleId,e.Insert.Action,out message);
            if (roleModule != null && roleModule.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已具有[{1}]的权限", roleModule.ModuleId,roleModule.Action);
                return;
            }
        }

        private void CheckAvaliable(object sender, DataValidationEventArgs<RoleSiteModel> e)
        {
            if (e.Delete.Avaliable)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<RoleSiteModel> e)
        {
            RoleSiteFetcher fetcher = new RoleSiteFetcher();
            string message = "";
            RoleSiteModel roleModule = fetcher.GetByRoleIdAndSiteId( e.Insert.RoleId, e.Insert.SiteId, out message);
            if (roleModule != null && roleModule.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已具有[{1}]的权限", roleModule.RoleId, roleModule.SiteId);
                return;
            }
        }

        private void CheckAvaliable(object sender, DataValidationEventArgs<RoleUserModel> e)
        {
            if (e.Delete.Avaliable)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.UserId);
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<RoleUserModel> e)
        {
            RoleUserFetcher fetcher = new RoleUserFetcher();
            string message = "";
            RoleUserModel roleModule = fetcher.GetByRoleIdAndUserId(e.Insert.RoleId, e.Insert.UserId, out message,out rows);
            if (roleModule != null && roleModule.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已具有[{1}]的权限", roleModule.RoleId, roleModule.UserId);
                return;
            }
        }
    }
}
